var e = require("../../utils/api.js"), t = getApp();

Page({
    data: {
        painting: {},
        shareImage: "",
        item: {},
        goods: {},
        Width: "",
        Height: "",
        img: "",
        showIndex: !0,
        showCommission: !0,
        source: "",
        index_bg: "",
        fenxiao_bg: ""
    },
    onLoad: function(a) {
        var s = this;
        if (wx.showLoading({
            title: "正在生成海报",
            mask: !0
        }), wx.getSystemInfo({
            success: function(e) {
                s.setData({
                    Width: e.screenWidth,
                    Height: e.screenHeight
                });
            }
        }), "index" == a.source) {
            s.setData({
                showIndex: !1
            });
            (i = getCurrentPages())[i.length - 1];
            n = "";
            if (void 0 == (o = i[i.length - 2])) ; else {
                r = o.data;
                wx.getImageInfo({
                    src: r.index_share_set.img,
                    success: function(e) {
                        s.setData({
                            index_bg: e.path
                        });
                    }
                }), n = r.head.m_id + "#" + r.member.m_id;
            }
            e.sharegoods({
                openid: t.globalData.openid,
                scene: n
            }).then(function(e) {
                if (0 == e.errno) {
                    s.setData({
                        info: e.data
                    });
                    for (var t = e.data.goods, a = [], i = function() {
                        wx.getImageInfo({
                            src: e.data.img,
                            success: function(t) {
                                wx.getImageInfo({
                                    src: e.data.qrcode,
                                    success: function(e) {
                                        s.setData({
                                            index_bg: t.path,
                                            index_code: e.path
                                        }), s.eventDraw();
                                    }
                                });
                            }
                        });
                    }, o = 0; o < t.length; o++) s.getimgs(a, o, t);
                    var n = "", r = setInterval(function() {
                        if (a.length == t.length) {
                            clearInterval(r);
                            for (var e = 1; e < a.length; e++) for (var o = e; o > 0; o--) a[o].k < a[o - 1].k && (n = a[o], 
                            a[o] = a[o - 1], a[o - 1] = n);
                            s.setData({
                                imgs: a
                            }), i();
                        }
                    }, 50);
                }
            });
        }
        if ("commission" == a.source) {
            s.setData({
                showCommission: !1
            });
            var i = getCurrentPages(), o = (i[i.length - 1], i[i.length - 2]), n = "";
            if (void 0 == o) ; else {
                var r = o.data;
                s.setData({
                    qrcode: r.codeImg,
                    codeNum: r.codeNum
                }), wx.getImageInfo({
                    src: r.posterBg,
                    success: function(e) {
                        wx.getImageInfo({
                            src: s.data.qrcode,
                            success: function(t) {
                                s.drawfenxiaoposter(e.path, t.path);
                            }
                        });
                    }
                });
            }
        }
    },
    getimgs: function(e, t, a) {
        wx.getImageInfo({
            src: a[t].g_icon,
            success: function(a) {
                e.push({
                    path: a.path,
                    k: t
                });
            }
        });
    },
    drawText: function(e, t, a, s, i, o, n) {
        for (var r = arguments.length > 7 && void 0 !== arguments[7] ? arguments[7] : 0, d = 0, c = 0, h = 0, l = 0; l < t.length; l++) {
            if ((d += e.measureText(t[l]).width) > i) {
                if (++h == r && 0 != h) {
                    e.fillText(t.substring(c, l - 2) + "...", a, s);
                    break;
                }
                e.fillText(t.substring(c, l), a, s), s += o, d = 0, c = l;
            }
            l == t.length - 1 && e.fillText(t.substring(c, l + 1), a, s);
        }
    },
    circleImg: function(e, t, a, s, i) {
        e.save();
        var o = 2 * i, n = a + i, r = s + i;
        e.strokeStyle = "white", e.lineWidth = "1", e.arc(n, r, i, 0, 2 * Math.PI), e.clip(), 
        e.drawImage(t, a, s, o, o), e.restore();
    },
    eventDraw: function() {
        var e = this, a = e.data.info.goods, s = e.data.info.head, i = wx.createCanvasContext("shareCanvas"), o = parseFloat(this.data.Width / 375);
        o /= 2, i.drawImage(e.data.index_bg, 0, 0, this.data.Width, 1268 * o), e.circleImg(i, e.data.index_code, 530 * o, 980 * o, 75 * o), 
        i.beginPath();
        for (var n = 60 * o, r = 240 * o, d = 326 * o, c = 240 * o, h = a.length > 6 ? 6 : a.length, l = 0; l < h; l++) {
            l % 2 == 0 && (n = 60 * o, r = 280 * o + l / 2 * c), l % 2 == 1 && (n = 60 * o + d, 
            r = 280 * o + (l - 1) / 2 * c), i.drawImage(e.data.imgs[l].path, n, r, 152 * o, 152 * o), 
            i.font = "normal bold 12px 微软雅黑", i.setFillStyle("#343434"), i.setFontSize(24 * o), 
            e.drawText(i, a[l].g_name, n + 160 * o, r + 40 * o, 70, 28 * o, 24 * o, 2), i.setFillStyle("#787878"), 
            i.setFontSize(16 * o), i.fillText("￥" + a[l].g_old_price, n + 160 * o, r + 105 * o);
            var g = parseFloat(i.measureText("￥" + a[l].g_old_price).width);
            i.setStrokeStyle("#787878"), i.moveTo(n + 160 * o, r + 40 * o + 60 * o), i.lineTo(n + 160 * o + g, r + 40 * o + 60 * o), 
            i.setLineWidth(1 * o), i.stroke(), i.setFillStyle("#e33a4a"), i.setFontSize(24 * o);
            var f = parseFloat(i.measureText("￥" + a[l].priceArry[0]).width);
            i.fillText("￥" + a[l].priceArry[0], n + 160 * o, r + 40 * o + 74 * o + 26 * o), 
            i.setFontSize(16 * o), i.fillText("." + a[l].priceArry[1], n + 160 * o + f, r + 40 * o + 74 * o + 26 * o);
        }
        setTimeout(function() {
            i.setFontSize(24 * o), i.setFillStyle("#fff"), e.drawText(i, t.globalData.diy_community_name + "：" + s.m_head_shop_name, 74 * o, 1045 * o, 350 * o, 12, 24 * o), 
            i.fillText("团长：" + s.m_name, 74 * o, 1084 * o), e.drawText(i, "提货地点：" + s.m_head_address, 74 * o, 1120 * o, 350 * o, 16, 24 * o, 2), 
            i.restore(), i.closePath(), i.stroke(), i.draw(!1, function() {
                setTimeout(function() {
                    wx.canvasToTempFilePath({
                        x: 0,
                        y: 0,
                        width: parseFloat(e.data.Width),
                        height: parseFloat(1268 * o),
                        destWidth: parseFloat(6 * e.data.Width),
                        destHeight: parseFloat(1268 * o * 6),
                        canvasId: "shareCanvas",
                        fileType: "png",
                        quality: 1,
                        success: function(t) {
                            e.setData({
                                shareImage: t.tempFilePath
                            }), wx.hideLoading();
                        },
                        fail: function(e) {
                            wx.hideLoading();
                        }
                    });
                }, 200);
            }, 500);
        });
    },
    eventSave: function() {
        wx.saveImageToPhotosAlbum({
            filePath: this.data.shareImage,
            success: function(e) {
                wx.showToast({
                    title: "保存图片成功",
                    icon: "success",
                    duration: 2e3
                });
            }
        });
    },
    drawfenxiaoposter: function(e, t) {
        var a = this, s = a.data.codeNum, i = wx.createCanvasContext("shareCommission"), o = parseFloat(this.data.Width / 375);
        o /= 2, i.drawImage(e, 0, 0, this.data.Width, 1268 * o), i.drawImage(t, 265 * o, 705 * o, 220 * o, 220 * o), 
        i.font = "normal bold 12px 微软雅黑", i.setFillStyle("#333"), i.setFontSize(34 * o), 
        i.fillText(s, 291 * o, 668 * o), i.stroke(), i.draw(!1, function() {
            setTimeout(function() {
                wx.canvasToTempFilePath({
                    x: 0,
                    y: 0,
                    width: parseFloat(a.data.Width),
                    height: parseFloat(1268 * o),
                    destWidth: parseFloat(6 * a.data.Width),
                    destHeight: parseFloat(1268 * o * 6),
                    canvasId: "shareCommission",
                    fileType: "png",
                    quality: 1,
                    success: function(e) {
                        a.setData({
                            shareImage: e.tempFilePath
                        }), wx.hideLoading();
                    },
                    fail: function(e) {
                        wx.hideLoading();
                    }
                });
            }, 200);
        }, 500);
    },
    save: function() {
        var e = this;
        wx.showLoading({
            title: "保存中",
            mask: !0
        }), wx.getSetting({
            success: function(t) {
                t.authSetting["scope.writePhotosAlbum"] ? wx.saveImageToPhotosAlbum({
                    filePath: e.data.shareImage,
                    success: function() {
                        wx.hideLoading(), wx.showToast({
                            title: "保存成功"
                        });
                    },
                    fail: function() {
                        wx.hideLoading(), wx.showToast({
                            title: "保存失败",
                            icon: "none"
                        });
                    },
                    complete: function() {
                        wx.hideLoading(), e.setData({
                            showposter: !0
                        });
                    }
                }) : wx.authorize({
                    scope: "scope.writePhotosAlbum",
                    success: function() {
                        wx.saveImageToPhotosAlbum({
                            filePath: e.data.shareImage,
                            success: function() {
                                wx.hideLoading(), wx.showToast({
                                    title: "保存成功"
                                });
                            },
                            fail: function() {
                                wx.hideLoading(), wx.showToast({
                                    title: "保存失败",
                                    icon: "none"
                                });
                            },
                            complete: function() {
                                wx.hideLoading(), e.setData({
                                    showposter: !0
                                });
                            }
                        });
                    },
                    fail: function() {
                        wx.hideLoading(), e.setData({
                            openSet: !0
                        });
                    }
                });
            }
        });
    },
    measureTexts: function(e) {
        var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 10, a = 0;
        return (e = (e = String(e)).split("")).forEach(function(e) {
            /[a-zA-Z]/.test(e) ? a += 7 : /[0-9]/.test(e) ? a += 5.5 : /\./.test(e) ? a += 2.7 : /-/.test(e) ? a += 3.25 : /[\u4e00-\u9fa5]/.test(e) ? a += 10 : /\(|\)/.test(e) ? a += 3.73 : /\s/.test(e) ? a += 2.5 : /%/.test(e) ? a += 8 : a += 10;
        }), a * t / 10;
    }
});